package com.calsoft.Hops_Mobile_Tablet;

import android.app.*;

import android.app.FragmentTransaction;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.*;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.calsoft.Hops_Mobile_Tablet.base.App;
import com.calsoft.Hops_Mobile_Tablet.base.OpToursItemData;
import com.calsoft.Hops_Mobile_Tablet.component.AlertDialogFragment;
import com.calsoft.Hops_Mobile_Tablet.component.ServiceOverViewFragment;
import com.calsoft.Hops_Mobile_Tablet.component.ServiceScheduleFragment;
import com.calsoft.Hops_Mobile_Tablet.component.SimpleAlertDialogFragment;
import com.calsoft.Hops_Mobile_Tablet.sql.OpToursDbAdapter;
import com.calsoft.Hops_Mobile_Tablet.util.SharedPrefs;


/*
 * User: Minh Diep
 * Date: 6/25/13
 * Time: 2:20 PM
 */

public class ServiceOverViewActivity extends FragmentActivity implements
        ActionBar.TabListener, AlertDialogFragment.AlertDialogListener, SimpleAlertDialogFragment.AlertDialogListener {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;

    private TextView addWishlistBtn;
    private TextView bookNowBtn;
    private String landServiceCode = "";
    private String date = "";
    private String currentWishListStatus= "N";
    private int startTime;
    private int endTime;

    private SharedPrefs sPrefs;
    private int currentPage;
    private OpToursDbAdapter opToursDbAdapter;
    private OpToursItemData opToursItemData;

    public static final String SERVICE_OVER_VIEW_LAND_SERVICE_CODE = "com.calsoft.hops.mobile.service.over.view.land.service.code";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_service_overview);

        sPrefs = new SharedPrefs(getApplicationContext());
        currentPage = sPrefs.getInt(SharedPrefs.CURRENT_PAGE, 0);

        opToursDbAdapter = new OpToursDbAdapter(getApplicationContext());
        opToursDbAdapter.open();

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            if (currentPage == App.APP_MENU_HOME) {
                landServiceCode = extras.getString(OpToursFragment.OP_TOURS_LAND_SERVICE_CODE, "");
                date = extras.getString(OpToursFragment.OP_TOURS_DATE, "");
                startTime = extras.getInt(OpToursFragment.OP_TOURS_START_TIME, 0);
                endTime = extras.getInt(OpToursFragment.OP_TOURS_END_TIME, 0);
            } else if (currentPage == App.APP_MENU_OP_TOURS){
                landServiceCode = extras.getString(OpToursFragment.OP_TOURS_LAND_SERVICE_CODE, "");
            } else if (currentPage == App.APP_MENU_WISH_LIST){
                landServiceCode = extras.getString(WishListFragment.WISH_LIST_LAND_SERVICE_CODE, "");
            }
            sPrefs.putString(SharedPrefs.CURRENT_LAND_SERVICE_CODE, landServiceCode);
        } else {
            landServiceCode = sPrefs.getString(SharedPrefs.CURRENT_LAND_SERVICE_CODE, "");
        }

        Cursor cursor = opToursDbAdapter.fetchServiceByLandServiceCode(landServiceCode);
        cursor.moveToFirst();
        opToursItemData = new OpToursItemData(cursor);
        cursor.close();

        addWishlistBtn = (TextView) findViewById(R.id.service_add_wishlist_btn);
        bookNowBtn = (TextView) findViewById(R.id.service_book_now_btn);
        currentWishListStatus = opToursItemData.getWishList();
        if (currentWishListStatus.equals("Y")){
            addWishlistBtn.setText("Remove Wishlist");
        } else {
            addWishlistBtn.setText("Add Wishlist");
        }

        addWishlistBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentWishListStatus.equals("Y")){
                    DialogFragment dialog = new AlertDialogFragment(App.DIALOG_SERVICE_ADD_WISHLIST, "Remove this service from your wishlist?", "ABC");
                    dialog.show(getSupportFragmentManager(), "AlertDialogFragment");

                } else {
                    DialogFragment dialog = new AlertDialogFragment(App.DIALOG_SERVICE_ADD_WISHLIST, "Add this service to your wishlist?", "ABC");
                    dialog.show(getSupportFragmentManager(), "AlertDialogFragment");
                }
            }
        });

        bookNowBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentPage == App.APP_MENU_HOME){
                    Intent intent = new Intent(ServiceOverViewActivity.this, BookNowActivity.class);
                    intent.putExtra(OpToursFragment.OP_TOURS_LAND_SERVICE_CODE, landServiceCode);
                    intent.putExtra(OpToursFragment.OP_TOURS_DATE, date);
                    intent.putExtra(OpToursFragment.OP_TOURS_START_TIME, startTime);
                    intent.putExtra(OpToursFragment.OP_TOURS_END_TIME, endTime);
                    startActivity(intent);
                } else if (currentPage == App.APP_MENU_OP_TOURS || currentPage == App.APP_MENU_WISH_LIST){
                    DialogFragment dialog = new SimpleAlertDialogFragment("", "This functionality is under development!");
                    dialog.show(getSupportFragmentManager(), "AlertDialogFragment");
                }
            }
        });


        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // Create the adapter that will return a fragment for each of the three
        // primary sections of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(
                getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager
                .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(actionBar.newTab()
                    .setText(mSectionsPagerAdapter.getPageTitle(i))
                    .setTabListener(this));
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        opToursDbAdapter.close();
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onDialogPositiveClick(DialogFragment dialog, int dialogType, String strArg) {
        switch (dialogType) {
            case App.DIALOG_SERVICE_ADD_WISHLIST:
                if (currentWishListStatus.equals("Y")){
                    opToursDbAdapter.updateWishList(landServiceCode, "N");
                    currentWishListStatus = "N";
                    addWishlistBtn.setText("Add Wishlist");
                } else {
                    opToursDbAdapter.updateWishList(landServiceCode, "Y");
                    currentWishListStatus = "Y";
                    addWishlistBtn.setText("Remove Wishlist");
                }

                dialog.dismiss();
                break;
            case App.DIALOG_SERVICE_BOOK_NOW:
                dialog.dismiss();
                break;
        }
    }

    @Override
    public void onDialogNegativeClick(DialogFragment dialog) {
        dialog.dismiss();
    }

    @Override
    public void onDialogPositiveClick(DialogFragment dialog) {
        dialog.dismiss();
    }

    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a DummySectionFragment (defined as a static inner class
            // below) with the page number as its lone argument.
            switch (position) {
                case 0:
                    ServiceOverViewFragment fragment = new ServiceOverViewFragment();
                    Bundle bundle = new Bundle();
                    bundle.putString(SERVICE_OVER_VIEW_LAND_SERVICE_CODE, landServiceCode);
                    fragment.setArguments(bundle);
                    return fragment;
//                case 1:
//                    return new ServiceScheduleFragment();
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return 1;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "Overview";
//                case 1:
//                    return "Schedule";
            }
            return null;
        }
    }
}